package com.weipeep.common.controller;
import com.weipeep.common.domain.WiselyMessage;
import com.weipeep.common.domain.WiselyResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class WebSocketController {

    @Autowired
    private SimpMessageSendingOperations simpMessageSendingOperations;

    @MessageMapping("/welcome")//1
    @SendTo("/topic/getResponse")//2
    public WiselyResponse say(WiselyMessage message) throws Exception {
        return new WiselyResponse("Welcome, " + message.getName() + "!");
    }
    @ResponseBody
    @RequestMapping(path = "/send", method = RequestMethod.GET)
    public WiselyResponse send(String message) {
        simpMessageSendingOperations.convertAndSend("/topic/getResponse", new WiselyResponse(message));
        return new WiselyResponse(message);
    }
}